package com.bigdata.controller;

import com.bigdata.model.Result;
import com.bigdata.model.SearchModel;
import com.bigdata.mysql.entity.JkfwRole;
import com.bigdata.service.mysql.JkfwRoleMenuService;
import com.bigdata.service.mysql.JkfwRoleService;
import com.bigdata.model.AjaxResult;
import com.bigdata.model.AjaxResultList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JkfwRole的路由接口服务
 *
 * @author
 */
@RestController
@RequestMapping("/mge/adx")
public class JkfwRoleController {

    @Autowired
    JkfwRoleService jkfwRoleService;
    @Autowired
    JkfwRoleMenuService jkfwRoleMenuService;

    @RequestMapping("listrole")
    @ResponseBody
    public AjaxResultList queryPage(HttpServletRequest request, SearchModel searchModel) {
        Result res = jkfwRoleService.queryPage(searchModel);
        AjaxResultList resultList = AjaxResultList.page(res.getList());
        resultList.setTotal(res.getTotal());
        return resultList;
    }

    @RequestMapping("getRoleid")
    @ResponseBody
    public AjaxResult getRoleid(String roleid) {
        JkfwRole role = jkfwRoleService.selectByPrimaryKey(roleid);
        List<String> menus = jkfwRoleMenuService.queryMenuRole(roleid);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("role", role);
        map.put("menus", menus);
        if (role != null) {
            return AjaxResult.ok(map);
        }
        return AjaxResult.fail("角色不存在！");
    }

    @RequestMapping("ckrolename")
    @ResponseBody
    public AjaxResult ckRoelName(String id, String rolename) {
        return AjaxResult.ok(jkfwRoleService.ckRoleName(id, rolename));
    }

    @RequestMapping("addRole")
    @ResponseBody
    public AjaxResult addRole(JkfwRole role, String menus, HttpServletRequest request) {
        Result res = jkfwRoleService.addRole(role, menus);
        if (res.getCode() == Result.Code_Error) {
            return AjaxResult.fail(res.getMessage());
        }
        return AjaxResult.ok();
    }

    @RequestMapping("updateRole")
    @ResponseBody
    public AjaxResult updateRole(JkfwRole role, String menus, HttpServletRequest request) {
        Result res = jkfwRoleService.updateRole(role, menus);
        if (res.getCode() == Result.Code_Error) {
            return AjaxResult.fail(res.getMessage());
        }
        return AjaxResult.ok();
    }

    @RequestMapping("delRole")
    @ResponseBody
    public AjaxResult delRole(String id) {
        Result res = jkfwRoleService.delRole(id);
        return AjaxResult.ok();
    }
//	/** JkfwRoleService服务 */
//	@Autowired
//	private JkfwRoleService jkfwRoleService;
//
//	/**
//	 * 查询所有JkfwRole数据的方法
//	 * @param value
//	 * @return
//	 */
//	@GetMapping(value = "/JkfwRole", produces = {"application/json;charset=UTF-8"})
//	public String find(JkfwRole value) {
//		return jkfwRoleService.find(value);
//	}
//
//	/**
//	 * 通过id查询JkfwRole数据的方法
//	 * @param id
//	 * @return
//	 */
//	@GetMapping(value = "/JkfwRole/{id}", produces = {"application/json;charset=UTF-8"})
//	public String findOne(@PathVariable(name="id") Integer id) {
//		return jkfwRoleService.findOne(id);
//	}
//
//	/**
//	 * 插入JkfwRole属性不为空的数据方法
//	 * @param id
//	 * @return
//	 */
//	@PostMapping(value = "/JkfwRole", produces = {"application/json;charset=UTF-8"})
//	public String save(JkfwRole value) {
//		return jkfwRoleService.saveNotNull(value);
//	}
//
//	/**
//	 * 更新JkfwRole属性不为空的数据方法
//	 * @param id
//	 * @return
//	 */
//	@PutMapping(value = "/JkfwRole", produces = {"application/json;charset=UTF-8"})
//	public String update(JkfwRole value) {
//		return jkfwRoleService.updateNotNullById(value);
//	}
//
//	/**
//	 * 通过id删除JkfwRole数据方法
//	 * @param id
//	 * @return
//	 */
//	@DeleteMapping(value = "/JkfwRole/{id}", produces = {"application/json;charset=UTF-8"})
//	public String delete(@PathVariable(name="id") Integer id) {
//		return jkfwRoleService.deleteById(id);
//	}
}
